package com.shuqi.common.utils;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.shuqi.application.ShuqiApplication;
import com.shuqi.controller.R;

public class ToastUtil {

    private static Toast mToast;

    public static final void showMessage(final String message) {
        ShuqiApplication.getApplicationHandler().post(new Runnable() {

            @Override
            public void run() {
                if (mToast == null) {
                    mToast =
                            Toast.makeText(ShuqiApplication.getContext().getApplicationContext(),
                                    message, Toast.LENGTH_SHORT);
                }
                mToast.setText(message);
                mToast.show();
            }
        });
    }


    private static Toast toast = null;
    private static LayoutInflater inflater;
    private static View layout;
    private static TextView text;

    /**
     * 自定义Toast
     * 
     * @param activity
     * @param mesmessage
     */
    public static void showToastFullyCustomizable(final Activity activity, final String mesmessage) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                if (inflater == null) {
                    inflater = activity.getLayoutInflater();
                    layout =
                            inflater.inflate(R.layout.custom,
                                    (ViewGroup) activity.findViewById(R.id.llToast));
                    text = (TextView) layout.findViewById(R.id.tvTextToast);
                }

                if (toast == null) {
                    toast = new Toast(activity.getApplicationContext());
                    toast.setDuration(Toast.LENGTH_SHORT);
                    toast.setView(layout);
                    text.setText(mesmessage);
                } else {
                    text.setText(mesmessage);
                }
                toast.show();
            }
        };
        activity.runOnUiThread(r);
    }

}
